package com.test.config.authenticationConfig;

import com.alibaba.fastjson.JSONObject;
import com.test.entity.ResultEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Author: admin
 * Date: 2022/11/17 15:57
 * Describe: TODO
 */
public class MyEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        // 直接提示前端认证错误
        ResultEntity entity = new ResultEntity(-1, "操作失败", "认证错误");
        JSONObject json = (JSONObject) JSONObject.toJSON(entity);
        out.write(json.toString());
        out.flush();
        out.close();
    }
}

